package cn.lau.demo.controller;

import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/prod/")
public class ProductionController {

	@RequiresRoles("USER")
	@GetMapping
	String getAll() {
		List<String> data = new LinkedList<>();
		data.add("Shoes");
		data.add("T-Shirt");
		data.add("Pants");
		data.add("Glasses");
		data.add("Hat");

		Map<String, Object> result = new HashMap<>();
		result.put("code", 0);
		result.put("msg", "success");
		result.put("data", data);
		return JSON.toJSONString(result);
	}

	@RequiresRoles("ADMIN")
	@RequiresPermissions("prod:get")
	@GetMapping("{id}")
	String get(@PathVariable("id") Integer id) {
		Map<String, Object> result = new HashMap<>();
		result.put("code", 0);
		result.put("msg", "success");
		result.put("data", "商品编号为" + id + "的商品信息");
		return JSON.toJSONString(result);
	}

	@RequiresRoles("ADMIN")
	@RequiresPermissions("prod:add")
	@PostMapping
	String add(String prodName) {
		Map<String, Object> result = new HashMap<>();
		result.put("code", 0);
		result.put("msg", "添加成功");
		result.put("data", prodName);
		return JSON.toJSONString(result);
	}

	@RequiresRoles("ADMIN")
	@RequiresPermissions("prod:delete")
	@DeleteMapping("{id}")
	String remove(@PathVariable("id") Integer id) {
		Map<String, Object> result = new HashMap<>();
		result.put("code", 0);
		result.put("msg", "删除成功");
		result.put("data", "商品编号为" + id + "的商品信息");
		return JSON.toJSONString(result);
	}

	@RequiresRoles("ADMIN")
	@RequiresPermissions("prod:update")
	@PutMapping("{id}")
	String modify(@PathVariable("id") Integer id) {
		Map<String, Object> result = new HashMap<>();
		result.put("code", 0);
		result.put("msg", "修改成功");
		result.put("data", "商品编号为" + id + "的商品信息");
		return JSON.toJSONString(result);
	}
}
